package com.sgb.kjwl.view.ui.fragment.shop;

import android.content.Context;

import androidx.databinding.DataBindingUtil;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentShopMateriaChildBinding;
import com.sgb.kjwl.di.shopping.DaggerShoppingFragComponent;
import com.sgb.kjwl.di.shopping.ShoppingFragModule;
import com.sgb.kjwl.model.entity.ShopGoodsInfoEntity;
import com.sgb.kjwl.view.ui.activity.self.shop.ShopGoodsDetailActivity;
import com.sgb.kjwl.view.ui.activity.self.shop.ShopInfoActivity;
import com.sgb.kjwl.view.ui.adapter.shopping.ShopMateriaAdapter;
import com.sgb.kjwl.viewmodel.shopping.ShopMateriaChildModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

/**
 * @Author chenfangkai
 * 企业店铺 在售商品
 */
public class ShopMaterialChildFragment extends AppFragment {
    public static final String SHOP_ID = "shop_id";
    private ShopInfoActivity activity;
    private FragmentShopMateriaChildBinding binding;
    private ShopMateriaAdapter adapter;
    private List<ShopGoodsInfoEntity> stringList;
    private Map map;
    private String shopId;
    @Inject
    ShopMateriaChildModel viewModel;

    public static ShopMaterialChildFragment newInstance(String shopId) {
        ShopMaterialChildFragment fragment = new ShopMaterialChildFragment();
        Bundle bundle = new Bundle();
        bundle.putString(SHOP_ID, shopId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof ShopInfoActivity) {
            activity = (ShopInfoActivity) context;
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_shop_materia_child, container, false);
        return binding.getRoot();
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        init();
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerShoppingFragComponent.builder()
                .appComponent(appComponent)
                .shoppingFragModule(new ShoppingFragModule(this))
                .build()
                .inject(this);
    }

    private void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        shopId = (String) getArguments().get(SHOP_ID);
        if (adapter == null) {
            adapter = new ShopMateriaAdapter(getActivity(), null);
        }
        binding.rlvList.setLayoutManager(layoutManager);
        binding.rlvList.setAdapter(adapter);
        adapter.setOnItemClick(new AdapterOnItemClick<ShopGoodsInfoEntity>() {
            @Override
            public void onItemClick(ShopGoodsInfoEntity s, int position) {
//                MaterialDetailActivity.start(getActivity(), s.getId());
                ShopGoodsDetailActivity.start(getActivity(), s.getId(), "", s.getSkuid());
            }
        });

        binding.rlvList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.queryListByStore(map);
            }

            @Override
            public void onLoadMore() {
                viewModel.loadMore(map);
            }
        });
    }


    private void init() {
        map = new HashMap(2);
        map.put("id", shopId);
        map.put("channel", "material");
        viewModel.queryListByStore(map);
    }

    public void refresh(List<ShopGoodsInfoEntity> stringList, boolean isLast) {
        binding.emptyView.setVisibility(View.GONE);
        binding.rlvList.setVisibility(View.VISIBLE);
        binding.rlvList.setLoadingMoreEnabled(true);
        adapter.setDatas(stringList);
        binding.rlvList.refreshComplete();
        if (isLast) {
            binding.rlvList.setLoadingMoreEnabled(false);
        }
    }


    public void loadMore(List<ShopGoodsInfoEntity> stringList, boolean isLast) {
        adapter.addDatas(stringList);
        binding.rlvList.loadMoreComplete();
        if (isLast) {
            binding.rlvList.setLoadingMoreEnabled(false);
        }
    }

    public void empty() {
        binding.rlvList.refreshComplete();
        binding.emptyView.setVisibility(View.VISIBLE);
    }

    public void loadMoreComplete() {
        binding.rlvList.loadMoreComplete();
    }
}
